<?php
require_once 'include/inc.config.php';
require_once WACT_ROOT . '/db/db.inc.php';
require_once APP_ROOT.'model/common/utils.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/date.php';
require_once APP_ROOT.'model/common/parameters.php';
require_once APP_ROOT.'model/common/site.php';



class Division {


    function insertDivision(&$data) {
    	$site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
    	$enabled = $data->get('enabled');
    	if (!$enabled) $data->set('enabled',0);

    	$tz =& new MyTimeZone();
        $data->set('date_added', gmdate("Y-m-d H:i", $tz->timeNowLocal() ) );

        $Record =& DBC::NewRecord($data);
        return $Record->insertId('sport_stat_divisions',
            array('site_id','leagueID','divisionName', 'enabled', 'date_added'), 'divisionID');
    }



    function &getRecord($id) {
        $rs =& DBC::FindRecord("SELECT D.*, L.leagueName
        FROM sport_stat_divisions D
        left outer join sport_stat_leagues L
    	on D.leagueID = L.leagueID
        WHERE D.divisionID = ". DBC::makeLiteral($id));
        //$leagueName = $rs->get('leagueName');
        //$divisionName = $rs->get('divisionName');
        //if ($leagueName) {
        	//$divisionName = $leagueName.':'.$divisionName;
        //}
        //$rs->set('divisionName',$divisionName);
        return $rs;
    }

    function updateDivision($id, &$data) {
        $site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);

    	$enabled = $data->get('enabled');
    	if (!$enabled) $data->set('enabled',0);

        $Record =& DBC::NewRecord($data);
        $Record->update('sport_stat_divisions',
            array('site_id','divisionName', 'enabled','leagueID'),
            "divisionID=". DBC::makeLiteral($id));
    }







    function denyDivision($division_id) {
        $data = new DataSpace();
        $data->set('enabled',0);

    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_stat_divisions', array('enabled'),
            "divisionID=". DBC::makeLiteral($division_id));
    }

    function publishDivision($division_id) {
        $data = new DataSpace();
        $data->set('enabled',1);

    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_stat_divisions',
            array('enabled'), "divisionID=". DBC::makeLiteral($division_id));
    }



	function delete($id) {
        DBC::execute("DELETE FROM sport_stat_divisions WHERE divisionID=". DBC::makeLiteral($id));
    }

    function &getDivisionsList(&$Pager, &$request, &$responseModel,$options=null, $onlynew=false) {
        $site_id = Site::getSiteID();
     	//restore filter from session
     	Session::loadResponseModel($responseModel,'filter.divisions');
     	$sql = "select D.*, L.leagueName
     		from sport_stat_divisions D
     		left outer join sport_stat_leagues L
     		on D.leagueID = L.leagueID
     		";

     	if($options==null){
     		if ($onlynew) {
                $sql.=" where 1=1 and (D.enabled=0 or D.enabled is null)
                and D.site_id = ".DBC::makeLiteral($site_id)." ".Division::getDivisionsFilter($request, $responseModel)."
     		    order by L.leagueName, D.divisionName, D.date_added desc  ";
     		} else {
     			$sql.=" where 1=1
                and D.site_id = ".DBC::makeLiteral($site_id)." ".Division::getDivisionsFilter($request, $responseModel)."
     		 	order by ".Division::getDivisionsOrder($request, $responseModel);
     		}
            //print $sql;
        	return DBC::NewPagedRecordSet($sql, $Pager);
     	}else{
     		$sql.=" where 1=1
             and D.site_id = ".DBC::makeLiteral($site_id);
     		$sql.=" order by L.leagueName, D.divisionName, D.date_added desc ";
     		error_log("[SQL:]$sql");
     		//print $sql;
     		return DBC::NewPagedRecordSet($sql, $Pager);
     	}
     }



    //get the array of divisions for list
	function &getDivisionsForList() {
		$site_id = Site::getSiteID();
		$sql = "select D.*, L.leagueName  from sport_stat_divisions D
		left outer join sport_stat_leagues L
  		on D.leagueID = L.leagueID
		";
        $sql .= " where D.site_id =  ".DBC::makeLiteral($site_id);
        $sql.=" order by L.leagueName, D.divisionName, D.date_added desc ";
		$result =& DBC::NewRecordSet($sql);
		$result->reset();
		//$i=1;
        $divisions=array();

        while($result->next()) {
        		$id =  $result->get('divisionID');
        		$name = $result->get('divisionName');
                $leagueName = $result->get('leagueName');
                $divisions[$id]= $leagueName.'-'.$name;
        }
        //print $sql;
        return $divisions;
	}

     function getDivisionsFilter(&$request,&$responseModel) {
      	$where = '';
      	$filter_division_id = $responseModel->get('filter_division_id');
        $division_id = $responseModel->get('division_id');
      	if ($filter_division_id)  $where .= ' and D.divisionID = '.DBC::makeLiteral($division_id);

        $filter_title = $responseModel->get('filter_title');
        $title = $responseModel->get('title');
        if ($filter_title)  $where .= ' and D.divisionName like '.DBC::makeLiteral('%'.$title.'%').'  ';

        return $where;
      }

      function getDivisionsOrder(&$request,&$responseModel) {
      	$sortby = 'D.divisionName';
      	$sortby = $responseModel->get('sort');
      	if ($sortby) return 'D.'.$sortby;
      	else return 'D.divisionName';
      }


      function &getNewDivisionsList(&$Pager, &$request, &$responseModel) {
      	return Division::getDivisionsList($Pager, $request, $responseModel,null,true);
      }



     function &getDivisionsPublic($currentDivisionID) {
        $site_id = Site::getSiteID();

     	$sql = "select D.*, L.leagueName  from sport_stat_divisions D
		left outer join sport_stat_leagues L
  		on D.leagueID = L.leagueID
		";
     	$sql.=" where D.site_id = ".DBC::makeLiteral($site_id);
        $sql.=" order by D.divisionName ";
     	//print  $sql;
        $rs =& DBC::NewRecordSet($sql);

        $list_updated=array();

        $rs->reset();
        while ($rs->next()) {

        	$dataspace = $rs->export();

            $divisionID = $rs->get('divisionID');
            if ($divisionID==$currentDivisionID)   $dataspace['selected_show'] =  'selected';

            //print_r($dataspace);
            $divisionName = $rs->get('divisionName');
            $leagueName = $rs->get('leagueName');
            $dataspace['divisionName'] = $leagueName.'-'.$divisionName;
            $list_updated[$divisionID] = $dataspace;
        }


        $list_source_updated =& new ArrayDataSet($list_updated);

        return $list_source_updated;
     }

	 // get One division
     function getFirstDivisionID() {
        $site_id = Site::getSiteID();
    	return DBC::getOneValue("select min(D.divisionID) from sport_stat_divisions D where D.enabled=1
    	and  D.site_id = ".DBC::makeLiteral($site_id));

    }



}

?>
